/*
 * @Description: 登录模块表单校验
 * @Author: zhenqiang.hong
 * @Date: 2019-12-12 15:00:48
 * @Last Modified by: zhenqiang.hong
 * @Last Modified time: 2019-12-12 16:37:54
 */
/**
 * 校验手机号
 * @param {*} username 手机号
 */
function verifyUsername(username) {
  let usernamePromise = new Promise((resolve, reject) => {
    if (username === '') {
      let toastMsg = '请输入手机号码'
      reject(toastMsg)
    } else if (!/^1[3-9]\d{9}$/.test(username)) {
      let toastMsg = '手机号码格式不正确'
      reject(toastMsg)
    } else {
      resolve(username)
    }
  })
  return usernamePromise
}
/**
 * 校验验证码
 * @param {*} smscode 验证码
 */
function verifySmscode(smscode) {
  let smscodePromise = new Promise((resolve, reject) => {
    if (smscode === '') {
      let toastMsg = '请填写收到的验证码'
      reject(toastMsg)
    } else {
      resolve(smscode)
    }
  })
  return smscodePromise
}

/**
 * 校验密码
 * @param {*} password 密码
 */
function verifyPassword(password) {
  let passwordPromise = new Promise((resolve, reject) => {
    if (password === '') {
      let toastMsg = '请输入密码'
      reject(toastMsg)
    } else if (password.length < 6 || password.length > 20) {
      let toastMsg = '请输入密码(6-20)'
      reject(toastMsg)
    } else {
      resolve(password)
    }
  })
  return passwordPromise
}

/**
 * 校验用户协议
 * @param {Boolean} isSelectAgreement 是否勾选用户协议
 */
function verifyAgreement(isSelectAgreement) {
  let agreementPromise = new Promise((resolve, reject) => {
    if (!isSelectAgreement) {
      let toastMsg = '请阅读并同意《用户协议》'
      reject(toastMsg)
    } else {
      resolve(isSelectAgreement)
    }
  })
  return agreementPromise
}

/**
 * validatePromiseAll：表单校验
 * @param {Array} options.checkArray 校验表单数据[{type:'username',value:'18900000000'}]
 * @param {Function} options.successCallback 校验成功回调函数
 * @param {Function} options.errorCallback 校验失败回调函数
 *
  validatePromiseAll({
    checkArray: [{
      type: 'username',
      value: username
    },{
      type: 'smscode',
      value: smscode
    }, {
      type: 'password',
      value: password
    },{
      type: 'agreement',
      value: agreement
    }],
    successCallback: (dataArray) => {
      console.log(dataArray)
    },
    errorCallback: (error) => {
      console.log('表单校验失败：'+error)
      this.$toastMsg(error)
    }
  })
 */
function validatePromiseAll(options) {
  let promiseArray = []

  if (options.checkArray && options.checkArray.length > 0) {
    options.checkArray.forEach((item, index) => {
      switch (item.type) {
        case 'username':// 手机号输入框
          promiseArray.push(verifyUsername(item.value))
          break
        case 'smscode':// 验证码输入框
          promiseArray.push(verifySmscode(item.value))
          break
        case 'password':// 密码输入框
          promiseArray.push(verifyPassword(item.value))
          break
        case 'agreement':// 用户协议勾选框
          promiseArray.push(verifyAgreement(item.value))
          break
        default:
          break
      }
    })
  }
  Promise.all(promiseArray).then(options.successCallback).catch(options.errorCallback)
}

export { validatePromiseAll }
